const mongoose = require("mongoose");
const { Schema } = mongoose;

// 文章列表项的规则
const ArticleListItemSchema = new Schema({
  title: { type: String, required: true },
  // 简介
  summary: { type: String, required: true },
  // 作者昵称
  authorNickName: { type: String, required: true },
  // 作者email，相当于用户的唯一标识
  authorEmail: { type: String, required: true },
  // 浏览数
  views: { type: Number, default: 0 },
  // 点赞数
  likes: { type: Number, default: 0 },
  // 评论数
  commentCount: { type: Number, default: 0 },
});

// 文章详情规则
const ArticleDetailSchema = new Schema({
  // 标题
  title: { type: String, required: true },
  // 简介
  summary: { type: String, required: true },
  // 封面图片，暂时没有实现
  // coverImage: { type: String, required: true },
  // 作者昵称
  authorNickName: { type: String, required: true },
  // 作者email，相当于用户的唯一标识
  authorEmail: { type: String, required: true },
  // 文章内容
  content: { type: String, required: true },
  // 点赞数
  likes: { type: Number, default: 0 },
  // 浏览数
  views: { type: Number, default: 0 },
  // 文章创建时间
  createTime: { type: Date, default: Date.now },
  // 收藏数
  // collects: { type: Number, default: 0 },
  // 评论列表
  commentList: [
    {
      // 评论id
      _id: { type: Schema.Types.ObjectId, required: true },
      // 评论者昵称
      authorNickName: { type: String, required: false },
      // 评论者的email
      authorEmail: { type: String, required: true },
      // 评论者头像地址
      authorAvatar: { type: String, required: false },
      // 评论内容
      content: { type: String, required: true },
      // 评论时间
      createTime: { type: Date, default: Date.now },
      // 回复列表
      replyList: [
        {
          // 回复id
          _id: { type: Schema.Types.ObjectId, required: true },
          // 回复者昵称
          authorNickName: { type: String, required: false },
          // 回复者的email
          authorEmail: { type: String, required: true },
          // 回复者头像地址
          authorAvatar: { type: String, required: false },
          // 回复时间
          createTime: { type: Date, default: Date.now },
          // 回复内容
          content: { type: String, required: true },
        },
      ],
    },
  ],
});

module.exports = {
  ArticleListItemSchema,
  ArticleDetailSchema,
};
